<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="姓名" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="formInline.nickname" placeholder="昵称" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="formInline.role" placeholder="角色" :clearable="true">
          <el-option label="Develop" value="Develop"></el-option>
          <el-option label="Test" value="Test"></el-option>
          <el-option label="PM" value="PM"></el-option>
          <el-option label="Designer" value="Designer"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRow">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="multipleTable" :data="pageData" tooltip-effect="dark" style="width: 100%">
      <el-table-column prop="id" label="ID" width="120"></el-table-column>
      <el-table-column label="姓名" width="120">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120"></el-table-column>
      <el-table-column prop="role" label="角色" show-overflow-tooltip></el-table-column>
      <el-table-column label="描述" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-html="scope.row.describe"></span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button @click.native.prevent="deleteRow(scope.$index)" type="text" size="small">删除</el-button>
          <el-button
              @click.native.prevent="editRow(scope.$index,scope.row)"
              type="text"
              size="small"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10]"
        :page-size="1"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    ></el-pagination>
    <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        style="height: 750px;"
        append-to-body
    >
      <el-form :model="form">
        <el-form-item label="ID" :label-width="formLabelWidth">
          <el-input v-model="form.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 599px;">
            <el-option label="Develop" value="Develop"></el-option>
            <el-option label="Test" value="Test"></el-option>
            <el-option label="PM" value="PM"></el-option>
            <el-option label="Designer" value="Designer"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <!-- <el-input type="textarea" v-model="form.describe"></el-input> -->
          <quill-editor
              ref="myTextEditor"
              v-model="form.describe"
              :options="editorOption"
              @change="onEditorChange"
          ></quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="saveForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from "../../../api/index.js";
export default {
  name: "EmployeeInformation.vue",
  data() {
    return {
      tableData: [],
      tableData2: [],
      pageData: [],
      formInline: {
        name: "",
        nickname: "",
        role: ""
      },
      currentPage: 1,
      pagesize: 5,
      total: 0,
      form: {
        id: "",
        name: "",
        nickname: "",
        role: "",
        describe: ""
      },
      dialogFormVisible: false,
      dialogTitle: "",
      isEdit: "",
      formLabelWidth: "120px",
      editorOption: {
        placeholder: "编辑文章内容"
      }
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    loadData() {
      api.getList("/news/index").then(res => {
        console.log(res);
        this.tableData = res.articles;
        this.tableData2 = res.articles;
        this.handleData();
      });
    },
    onSubmit() {
      //筛选条件
      var filter = this.formInline;
      //拿到有值的参数
      var tempFilter = {};
      for (let key in filter) {
        if (
            typeof filter[key] != "undefined" &&
            typeof filter[key] != "null" &&
            filter[key] != null &&
            filter[key] != ""
        ) {
          tempFilter[key] = filter[key];
        }
      }
      if (JSON.stringify(tempFilter) == "{}") {
        this.tableData = this.tableData2;
      } else {
        //筛选
        this.tableData = this.tableData2.filter(item => {
          var flag = false;
          for (let key in tempFilter) {
            if (item[key].toString().indexOf(tempFilter[key].toString()) >= 0) {
              flag = true;
            } else {
              flag = false;
              break;
            }
          }
          if (flag) {
            return item;
          }
        });
      }
      this.handleData();
    },
    deleteRow(index) {
      this.tableData.splice(index, 1);
      this.tableData2 = this.tableData;
      this.handleData();
    },
    handleData() {
      this.total = this.tableData.length;
      this.pageData = this.tableData.slice(
          (this.currentPage - 1) * this.pagesize,
          this.currentPage * this.pagesize
      );
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.handleData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handleData();
    },
    editRow(index, row) {
      this.dialogFormVisible = true;
      this.form = row;
      this.dialogTitle = "修改";
      this.isEdit = true;
    },
    addRow() {
      this.form = {};
      this.dialogFormVisible = true;
      this.dialogTitle = "新增";
      this.isEdit = false;
    },
    saveForm() {
      if (this.isEdit) {
        this.tableData.forEach(element => {
          if (element.id == this.form.id) {
            element = this.form;
          }
        });
      } else {
        this.tableData.push(this.form);
        this.tableData2 = this.tableData;
      }

      this.close();
      this.handleData();
    },
    close() {
      this.dialogFormVisible = false;
    },
    onEditorChange({ editor, html, text }) {
      this.form.describe = html;
    }
  }
};
</script>

<style >
.quill-editor /deep/ .ql-container.ql-snow {
  border: 1px solid #ccc;
  min-height: 100px;
}

.el-dialog__body {
  padding: 30px 20px;
  color: #606266;
  font-size: 14px;
  word-break: break-all;
  height: 500px;
}
</style>
